package org.metasketch.editor;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ToolBar;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public abstract class Editor extends Application {

	protected Stage stage;

	protected Scene scene;

	protected BorderPane left = new BorderPane();

	protected BorderPane right = new BorderPane();

	protected BorderPane center = new BorderPane();

	protected BorderPane bottom = new BorderPane();

	protected BorderPane top = new BorderPane();

	protected ToolBar toolbar = new ToolBar();

	protected Project project;

	public void setDirty(boolean state) {
		project.setDirty(state);
		refreshTitle();
	}

	protected void refreshTitle(String msg, boolean dirty) {
		stage.setTitle((dirty ? "*" : "") + msg + " - DEMO Simulator V0.0.1");

	}

	public void refreshTitle() {

		String path = project.getPath();
		if (path != null) {
			String name = path.substring(path.lastIndexOf('\\') + 1);

			refreshTitle(name, project.isDirty());
		} else {
			refreshTitle("untitled", true);
		}
	}

	public void setup() throws Throwable {
		// refreshTitle("untitled", true);

		BorderPane panel = new BorderPane();

		// Create a scrollbar using JScrollPane and add panel into it's viewport
		// Set vertical and horizontal scrollbar always show
		// ScrollPane scrollPanel = new ScrollPane();
		// scrollPanel.setContent(panel);
		// scrollPanel.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
		// scrollPanel.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);

		scene = new Scene(panel, 1024, 768, Color.GRAY);

		panel.setTop(top);
		panel.setCenter(center);
		panel.setBottom(bottom);
		panel.setLeft(left);
		panel.setRight(right);

	}

	// protected abstract Project setupDefaultProject() throws Throwable;

	@Override
	public void start(Stage stage) {
		this.stage = stage;
		// Group container = new Group();
		try {
			setup();

			// setTitle("untitled");
			stage.setScene(scene);
			// stage.setVisible(true);
			// stage.setFullScreen(true);

			// setup();
			stage.show();

		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void setProject(Project project) {

		this.project = project;

		cleanInterface();

		refreshTitle();
		loadProject();
	}

	protected abstract void loadProject();

	protected abstract void cleanInterface();

	public BorderPane getLeft() {
		return left;
	}

	public BorderPane getRight() {
		return right;
	}

	public BorderPane getCenter() {
		return center;
	}

	public BorderPane getBottom() {
		return bottom;
	}

	public BorderPane getTop() {
		return top;
	}

	public ToolBar getToolbar() {
		return toolbar;
	}

	public Project getProject() {
		return project;
	}

	public abstract void resetLayout();

}
